﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.Model;
using CustomsSystem.BLL;
using System.Data;
using Maticsoft.Common;

namespace WEB.contract
{
    public partial class Add : System.Web.UI.Page
    {
        ContractManage bll = new ContractManage();
        CustomerManage cusBll = new CustomerManage();
        DataSet ds = new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.tbDate1.Attributes.Add("onClick", "new WdatePicker()");
                this.tbDate2.Attributes.Add("onClick", "new WdatePicker()");//控件绑定
                //数据载入
                if (Request.QueryString["AddID"] != null)
                {
                    string cID = Request.QueryString["AddID"].ToString();
                    Customer cusModel = cusBll.GetModel(Convert.ToInt32(cID));
                    tbCID.Text = cusModel.CustomerName.ToString();
                    tbCID.ReadOnly = true;
                }

            }
        }

       

        protected void btSave_Click(object sender, EventArgs e)
        {
            Customer cusModel = cusBll.GetModel(tbCID.Text.ToString());
            Cu_Contract model = new Cu_Contract();
            //验证
            string msg="";
            //if (tbNO.Text == "")
            //{
            //    msg += "合同号不能为空！ \\n";
            //}
            
            if(tbDate1.Text=="")
            {
                msg+="日期不能为空！ \\n";
            }
            else if (tbDate2.Text!="")
            {
                if(Convert.ToDateTime(tbDate1.Text) > Convert.ToDateTime(tbDate2.Text))
                {
                    msg += "结束日期小于开始日期！ \\n";
                }
            
            }

           if(FileUpload.FileName=="")
           {
               msg += "请选择上传文件！ \\n";
           }
            if (tbRemarks.Text.Length > 100)
            {
                msg+= "备注文本超过规定长度！ \\n";
            }
            if(msg!="")
            {
                MessageBox.Show(this,msg);
                return;
            }
            //载入数据
            model.CID = cusModel.ID;
            model.ContractNO = tbNO.Text.Trim();
            model.Datestart = Convert.ToDateTime(tbDate1.Text);

            if(tbDate2.Text.Trim()!="")
            {
                model.Dateover = Convert.ToDateTime(tbDate2.Text);
            }

            model.Images = Upload(model.ContractNO+tbCID.Text);//返回上传地址

            if(model.Images=="")
                return;

            model.Remarks = tbRemarks.Text.Trim();
            model.inputDate = DateTime.Now;

            if (bll.Add(model)>0)
            {
                MessageBox.ShowAndRedirect(this, "增加成功", "/contract/View.aspx?ID="+model.CID);
            }
            else
            {
                MessageBox.Show(this,"增加失败");
            }

        }
        /// <summary>
        /// 上传处理
        /// </summary>
        private string Upload(string id)
        {
            //文件判断
            string msg = "";
            /*通过文件扩展名判断文件类型*/
            string fileExt = System.IO.Path.GetExtension(FileUpload.FileName);
            if (fileExt != ".doc" && fileExt != ".jpg" && fileExt != ".pdf" && fileExt != ".zip" && fileExt != ".rar" && fileExt != ".bmp")
            {
                msg += "文件类型错误！应为：doc、jpg、bmp、zip、rar或pdf";
            }
            /*限制文件大小*/
            if (FileUpload.PostedFile.ContentLength > 20 * 1024 * 1024)
            {
                msg += "文件应小于20MB";
            }
            if (msg != "")  //没有任何不妥
            {
                //MessageBox.ShowAndRedirect(this, msg, Request.Url.LocalPath.ToString());
                MessageBox.Show(this, msg);
                return "";
            }
            else
            {

                //文件地址处理
                string localpath = Server.MapPath("~/document/contract/");
                string fullpath = localpath + id + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
                fullpath = Server.HtmlEncode(fullpath);
                FileUpload.SaveAs(fullpath);//文件上传
                return fullpath;
            }
        }
    }
}